<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Home extends CI_Controller {

    public $layout = DEFAULTLAYOUT;

    function __construct() {
        parent::__construct();

        $this->load->language('home');

        if($this->router->method == 'index' OR $this->router->method == 'maintenance')
            $this->layout = 'blank';
    }

    function index() {
        $data['message'] = $this->session->flashdata('message');
        $this->load->view('home', $data);
    }

    function main () {

        if($this->input->post('submit') != NULL) {
            $post = $this->input->post();
            unset($post['submit']);

            $news_item_read = new News_item_read();
            $news_item_read->where('news_item_id', $post['news_item_id'])->get();
            $news_item_read->news_item_id = $post['news_item_id'];

            $users_id = explode('-', $news_item_read->users_id);
            $users_id[] = get_user_id();
            $news_item_read->users_id = implode('-', $users_id);

            db_save($news_item_read);

            redirect('home/main');
        }

        $language = get_lang('label');
        $data['title'] = $language.'_title';

        $this->load->model('user_panel/user');
        $user = new User();
        $user->where('id', get_user_id())->get();

        $news_item = new News_item();
        $news_item->where('date >=', $user->created)->order_by('date', 'ASC')->get();

        $last_news_item_id = NULL;
        foreach ($news_item as $row) {
            unset($news_item_read);
            $news_item_read = new News_item_read();
            $user_id = get_user_id();

            $news_item_read->like('users_id', $user_id.'-%');
            $news_item_read->or_like('users_id', '%-'.$user_id.'-%');
            $news_item_read->or_like('users_id', '%-'.$user_id);
            $count = $news_item_read->where('news_item_id', $row->id)->count();

            if($count == 0) {
                $last_news_item_id = $row->id;
                break;
            }
        }

        if($last_news_item_id == NULL) {
            $data['latest_news'] = $news_item->order_by('date', 'DESC')->get(3);
            $data['total_unread_news'] = 0;
        } else {

            $last_news_item = new News_item();
            $data['last_news'] = $last_news_item->where('id', $last_news_item_id)->get();
            $data['last_news_text'] = $language.'_news';

            $data['total_unread_news'] = $news_item->where('date >=', $last_news_item->date)->count();
        }

        $changelog = new Changelog();
        $data['changelog'] = $changelog->order_by('date', 'DESC')->get();
        $data['changelog_text'] = $language.'_changelog';

        $development = new Development();
        $data['development'] = $development->order_by('display_order', 'ASC')->get();
        $data['development_text'] = $language.'_development';

        $this->load->view('main', $data);
    }

    function news($news_item_id = NULL) {

        $language = get_lang($this);
        $data['title'] = $language.'_title';
        $data['news_text'] = $language.'_news';

        $selected_news_item = new News_item();
        $data['selected_news'] = $selected_news_item->where('id', $news_item_id)->get();
        $news_item = new News_item();
        $data['news'] = $news_item->order_by('date', 'DESC')->get();

        $this->load->view('news', $data);
    }

    function changelog($changelog_id = NULL) {

        $language = get_lang($this);
        $data['title'] = $language.'_title';
        $data['changelog_text'] = $language.'_changelog';

        $changelog = new Changelog();
        $data['changelog'] = $changelog->order_by('date', 'DESC')->get();

        $selected_changelog = new Changelog();
        $data['selected_changelog'] = $selected_changelog->where('id', $changelog_id)->get();

        $this->load->view('changelog', $data);
    }

    function development($development_id = NULL) {

        $language = get_lang($this);
        $data['title'] = $language.'_title';
        $data['development_text'] = $language.'_development';

        $development = new Development();
        $data['development'] = $development->order_by('display_order', 'ASC')->get();

        $selected_development = new Development();
        $data['selected_development'] = $selected_development->where('id', $development_id)->get();

        $this->load->view('development', $data);
    }

    function message () {
        $data['message'] = $this->session->flashdata('message');

        $this->load->view('message', $data);
    }

    function IE() {
        $this->layout = 'blank';
        $this->load->view('IE');
    }

    function maintenance () {
        $this->load->view('maintenance');
    }
}

/* End of file home.php */
/* Location: ./application/modules/home/controllers/home.php */